<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Redis 主从复制 | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/48.7d356e5b.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Redis 主从复制</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">Redis 主从复制</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>4/20/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Redis</span></i></div></div> <div class="theme-reco-content content__default"><blockquote><p>Redis的高可用，采用的Redis多机 和 集群的方式。 多机保证数据的一致性，就会主从复制出现。</p> <p>常见的方案： 一主一从、一主多从</p></blockquote> <p><strong>主从复制的作用</strong></p> <ul><li>数据冗余： 主从复制实现了数据的热备份，是持久化以外的另一种数据冗余</li> <li>故障恢复： 当主节点数出现问题是，可以由从节点提供服务，实现快速的故障恢复</li> <li>读写分离： 实现读写分离，提高 Redis 的吞吐量</li> <li>负载均衡： 通过 主从复制的基础上，配合读写分离，可以对每个节点进行负载均匀分配，尤其是在写少读多的场景下，通过多个从节点分担读负载，可以大大提高Redis服务器的并发量</li></ul> <p><strong>如何配置</strong></p> <p>从节点开启主从复制，有3种方式：</p> <ol><li>配置文件： 在从服务器的配置文件 <strong>redis.conf</strong> 中加入：<code>slaveof &lt;masterip&gt; &lt;masterport&gt;</code></li> <li>启动命令： redis-server启动命令后加入 <code>—slaveof &lt;masterip&gt; &lt;masterport&gt;</code></li> <li>客户端命令： Redis服务器启动后，直接通过客户端执行命令：<code>slaveof &lt;masterip&gt; &lt;masterport&gt;</code>，则该Redis实例成为从节点。</li></ol> <h3 id="复制功能的实现"><a href="#复制功能的实现" class="header-anchor">#</a> 复制功能的实现</h3> <blockquote><p>Redis 的 复制功能 主要分为两大功能 <code>同步</code>  和 <code>命令传播</code></p></blockquote> <ul><li>Redis 2.8  之前 使用 同步（SYNC） +  命令传播</li> <li>Redis 2.8  之后 使用 同步  (PSYNC)  + 命令传播</li></ul> <p><strong>同步</strong>：即 从服务器 向 主服务器 发送复制命令，从而进行数据复制</p> <p><strong>命令传播</strong>: 当完成所有数据复制后，后续 主服务器的数据变更，将 同步修改从服务器的数据</p> <h3 id="部分复制的实现"><a href="#部分复制的实现" class="header-anchor">#</a> 部分复制的实现</h3> <blockquote><p>部分数据复制同步的实现，主要是依赖 <code>主/从服务器的偏移量</code>、<code>主服务器的复制积压缓冲区</code> 、<code>服务器运行ID</code></p></blockquote> <p><strong>复制偏移量</strong></p> <p>主/从服务器 各自维护一个复制偏移量</p> <p>主服务器：向 从服务器传播N个数据时, 偏移量就会加N</p> <p>从服务器：收到 主服务器传播N个数据时，偏移量就会加N</p> <p>只需要比对 两个服务器的偏移量，就可以知道两者数据是否一致</p> <p>并且 当从服务器断连恢复后，会根据 偏移量 和 复制积压缓冲区 判断是 全量复制 还是 部分复制</p> <p><strong>复制积压缓冲区</strong></p> <blockquote><p><code>复制积压缓冲区</code> 是由主服务器维护的一个 <strong>固定长度(fixed-size)</strong> <strong>先进先出(FIFO)队列</strong>,  默认大小为1MB</p></blockquote> <img src="/oliver-vuepress/middleware/redislearn/image-20220404163241394.png" alt="foo"> <p>当 主服务器 命令传播时，除了 发送信息给 所有的从服务器，<strong>并且会将命令写入到复制积压缓冲区</strong></p> <p><strong>服务器重新连接后</strong></p> <p>从服务器会通过PSYNC命令将自己的复制偏移量offset发送给主服务器。</p> <p>主服务器进行判断：offset 之后的数据是否 任然存在于 缓冲区里</p> <ul><li>存在：部分复制</li> <li>不存在：全量复制</li></ul> <p><strong>如何合理的设置缓冲区大小</strong></p> <p>最小缓冲区设置公式：<code>second * write_size_per_second</code></p> <ul><li>second： 服务器重新连接的平均时间</li> <li>write_size_per_second： 主服务器平均每秒产生的写命令数据量</li></ul> <p>一般为了保险起见，建议设置成2倍：<code>2 * second * write_size_per_second</code></p> <p><strong>服务运行ID</strong></p> <blockquote><p>每个Redis服务器,不论主服务器还是从服务,都会有自己的运行ID</p></blockquote> <p>初次复制时，主服务器 会将自己的 运行ID 发送给 从服务器， 从服务器会保存起来。用于判断同步数据的主服务器是否一致</p> <p>当服务器重新连接后：判断 主服务运行ID是否一致：</p> <ul><li>一致：进行验证是否能执行部分复制</li> <li>不一致：进行全量复制，并更新 主服务运行ID</li></ul> <h3 id="psync-指令流程"><a href="#psync-指令流程" class="header-anchor">#</a> PSYNC 指令流程</h3> <img src="/oliver-vuepress/middleware/redislearn/image-20220404171047915.png" alt="foo"> <h3 id="具体执行流程"><a href="#具体执行流程" class="header-anchor">#</a> 具体执行流程</h3> <img src="/oliver-vuepress/middleware/redislearn/image-20220404205134261.png" alt="foo"> <p><strong>1. 保存主节点信息</strong></p> <p>根据相关配置 或 操作命令， 从服务器 将 主服务器的连接方式 保存到 <code>RedisServer</code>  中</p> <div class="language-c# extra-class"><pre class="language-text"><code>Struct redisServer{
	char *masterhost;//主服务器ip
	int masterport;//主服务器端口
} ;
</code></pre></div><p><strong>2. 建立socket连接</strong></p> <blockquote><p>从服务器将根据命令所设置的IP地址和端口,创建连向主服务器的套接字(socket)连接</p></blockquote> <p>从服务器：与主服务器创建连接后，关联一个用于复制工作的文件事件处理器</p> <p>主服务器：会将 从服务器当做客户端来对待</p> <p><strong>3 发送ping命令</strong></p> <blockquote><p>建立连接成功后节点发送 <code>ping</code> 进行首次通信操作</p></blockquote> <p>其目的：</p> <ul><li>检测主从之间网络socket连接是否正常</li> <li>检测主服务器能否正常处理请求</li></ul> <img src="/oliver-vuepress/middleware/redislearn/image-20220404205148712.png" alt="foo"> <p><strong>ping命令的三种情况</strong></p> <ul><li>主服务器返回错误，不执行后续操作</li> <li>主服务器获取到命令，但是无法给其回复，不执行后续操作</li> <li>主服务器返回 <code>PONG</code>回复</li></ul> <p><strong>4. 身份验证</strong></p> <blockquote><p>当 服务器开启了 身份验证 才会起到作用</p></blockquote> <img src="/oliver-vuepress/middleware/redislearn/image-20220404205953332.png" alt="foo"> <p><strong>5.同步数据</strong></p> <blockquote><p>主从建立连接后，主节点全量数据发送给从节点，进行复制同步</p></blockquote> <p><strong>同步数据分为：</strong></p> <ul><li>全量复制</li> <li>增量复制</li></ul> <p><strong>版本之间的区别</strong></p> <blockquote><p>Redis 2.8  之前 使用 同步（SYNC） +  命令传播
Redis 2.8  之后 使用 同步  (PSYNC)  + 命令传播</p></blockquote> <p><strong>为什么这么优化？</strong></p> <p><code>SYNC</code>   没有全量复制 和 部分复制的机制，每次同步时，从服务都会进行全量复制，并清除原有的数据</p> <p><strong>2.8之前的同步过程</strong></p> <img src="/oliver-vuepress/middleware/redislearn/image-20220404210359578.png" alt="foo"> <p><strong>2.8之后的同步过程</strong></p> <ul><li>断线重连后的 恢复 模式  时 会进行判断  全量同步 还是 增量同步</li></ul> <img src="/oliver-vuepress/middleware/redislearn/image-20220404210506790.png" alt="foo"> <p><strong>6.命令传播</strong></p> <blockquote><p>当主节点同步数据完成后，会进行命令传播阶段，持续同步后续的数据，保证服务器一直保证一致</p></blockquote> <p>命令传播 主要利用了 心跳检测 机制 进行 主从之间的信息交互，并用来维护双方保持在线的的功能</p> <h3 id="心跳检测"><a href="#心跳检测" class="header-anchor">#</a> 心跳检测</h3> <p><strong>主要的作用</strong></p> <ul><li>检测主从服务的网络状态</li> <li>辅助实现 <code>min-slaves</code>选项</li> <li>检测命令丢失</li></ul> <p>主服务器心跳：</p> <ul><li>指令：PING</li> <li>周期：通过repl-ping-slave-period配置，默认时10秒。</li> <li>作用：判断slave是否在线</li> <li>查询：可以通过info replication查看，获取slave最后一次的连接间隔，lag是0或者1是正常的，超过1就可能是超时了</li></ul> <p>从服务器心跳：</p> <ul><li>命令： <code>REPLCONF ACK &lt;replication_offset&gt;</code></li> <li>周期：1秒</li> <li>作用1：告诉master自己的复制偏移量，获取最新的数据变更指令</li> <li>作用2：判断master是否在线</li></ul> <h3 id="相关好文"><a href="#相关好文" class="header-anchor">#</a> 相关好文</h3> <ul><li><a href="https://www.cnblogs.com/wangcuican/p/12915856.html" target="_blank" rel="noopener noreferrer">Redis入门到精通（十九）——主从复制：数据同步+命令传播阶段工作流程，主从复制工作流程（完整），主从复制常见问题 - 只会玩辅助 - 博客园<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zhuanlan.zhihu.com/p/112213386" target="_blank" rel="noopener noreferrer">Redis主从复制以及主从复制原理 - 知乎<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.cnblogs.com/wangcuican/p/12915856.html" target="_blank" rel="noopener noreferrer">Redis入门到精通（十九）——主从复制：数据同步+命令传播阶段工作流程，主从复制工作流程（完整），主从复制常见问题 - 只会玩辅助 - 博客园<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>  【文章中指出了 -  很多主从复制的问题】</li> <li><a href="https://www.cnblogs.com/wangcuican/p/12913608.html" target="_blank" rel="noopener noreferrer">Redis入门到精通（十八）——主从复制阶段二：数据同步阶段工作流程、数据同步阶段注意事项，主从复制阶段三：命令传播阶段的部分复制 - 只会玩辅助 - 博客园<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6.html#复制功能的实现" class="sidebar-link reco-side-复制功能的实现" data-v-cb1513f6>复制功能的实现</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6.html#部分复制的实现" class="sidebar-link reco-side-部分复制的实现" data-v-cb1513f6>部分复制的实现</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6.html#psync-指令流程" class="sidebar-link reco-side-psync-指令流程" data-v-cb1513f6>PSYNC 指令流程</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6.html#具体执行流程" class="sidebar-link reco-side-具体执行流程" data-v-cb1513f6>具体执行流程</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6.html#心跳检测" class="sidebar-link reco-side-心跳检测" data-v-cb1513f6>心跳检测</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6.html#相关好文" class="sidebar-link reco-side-相关好文" data-v-cb1513f6>相关好文</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/48.7d356e5b.js" defer></script>
  </body>
</html>
